//
//  GQHQutationsView.m
//  Xanthium
//
//  Created by GuanQinghao on 09/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHQutationsView.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"

@interface GQHQutationsView () <UITableViewDelegate,UITableViewDataSource,GQHQutationCellDelegate>

@end


@implementation GQHQutationsView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _qh_qutationModelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 225.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    GQHQutationCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[GQHQutationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.qh_delegate = self;
    cell.qh_dialButton.tag = indexPath.row;
    cell.qh_handleButton.tag = indexPath.row;
    cell.qh_canclePrince.tag = indexPath.row;
    
    GQHQutationModel *qutationModel = _qh_qutationModelArray[indexPath.row];
    
    cell.qh_routeLabel.text = [NSString stringWithFormat:@"%@-%@",qutationModel.qh_departure,qutationModel.qh_destination];
    cell.qh_amountLabel.text = [NSString stringWithFormat:@"报价:%@",qutationModel.qh_amountString];
    cell.qh_contentLabel.text = [NSString stringWithFormat:@"%@/%.0f吨/%@/%@",qutationModel.qh_freightName,qutationModel.qh_freightWeight,qutationModel.qh_model,qutationModel.qh_lengthWanted];
    cell.qh_loadingTimeLabel.text = [NSString stringWithFormat:@"装车时间:%@",qutationModel.qh_loadingTime];
    cell.qh_loaderLabel.text = [NSString stringWithFormat:@"装车联系人:%@",qutationModel.qh_loader];
    cell.qh_mileageLabel.text = [NSString stringWithFormat:@"估算里程:%@公里",qutationModel.qh_mileage];
    
    if ([qutationModel.qh_quoteState isEqualToString:@"01"])
    {
        cell.qh_statusLabel.text = @"已报价";
        if (qutationModel.qh_freightWeight == 0)
        {
            cell.qh_showStatus.text = @"等待货主确认";
            [cell.qh_handleButton setTitle:@"重新报价" forState:UIControlStateNormal];
            cell.qh_handleButton.backgroundColor = kAppGreenColor;
        }
        else
        {
            cell.qh_showStatus.text = @"";
            [cell.qh_handleButton setTitle:@"货源已下架" forState:UIControlStateNormal];
            cell.qh_handleButton.backgroundColor = [UIColor grayColor];
            cell.qh_handleButton.enabled = NO;
        }
        [cell.qh_handleButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(90);
        }];
        
        [cell.qh_canclePrince mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(80);
        }];
        /// 已报价 01
    }
    if ([qutationModel.qh_quoteState isEqualToString:@"02"])
    {
        cell.qh_showStatus.text = @"等待货主确认";
        cell.qh_statusLabel.text = @"已中标";
        /// 已中标 02
        cell.qh_handleButton.backgroundColor = kAppGreenColor;
        [cell.qh_handleButton setTitle:@"查看协议" forState:UIControlStateNormal];
        [cell.qh_canclePrince mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0);
        }];
        [cell.qh_handleButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(90);
        }];
    }
    if ([qutationModel.qh_quoteState isEqualToString:@"03"])
    {
        cell.qh_showStatus.text = @"";
        cell.qh_statusLabel.text = @"已承运";
        /// 已承运 03
        [cell.qh_handleButton setTitle:@"" forState:UIControlStateNormal];
        
        [cell.qh_handleButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0);
        }];
        
        [cell.qh_canclePrince mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0);
        }];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)qh_dialMobile:(UIButton *)sender {
    DLog(@"联系货主");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_dialMobile:)]) {
        
        [_qh_delegate qh_dialMobile:sender];
    }
}

- (void)qh_acceptContractOrNot:(UIButton *)sender {
    DLog(@"确认承运协议或取消报价");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_acceptContractOrNot:)]) {
        
        [_qh_delegate qh_acceptContractOrNot:sender];
    }
}

- (void)qh_canclePrince:(UIButton *)sender
{
    if ([_qh_delegate respondsToSelector:@selector(qh_canclePrice:)]) {
        
        [_qh_delegate qh_canclePrice:sender];
    }
}

@end
